const app = getApp();
Page({
  data: {
    userinfo: {},
    active: 1,

    itemList: [],
    noMore: 0,
    page: 1,
  },
  onLoad: function (options) {

  },
  onShow() {
    this.refreshList()
    this.getUserInfo();
  },
  getList() {
    let page = this.data.page
    let itemList = this.data.itemList
    let active = this.data.active;
    let url,datas
    if(active == 1){
      url = '/huahua/zuopin';
      datas = {
        page: page
      }
    }else if (active == 2) {
      url = '/user/my_like';
      datas = {
        page: page,
        type: 1
      }
    } else if (active == 3) {
      url = '/user/my_like';
      datas = {
        page: page,
        type: 2
      }
    } else if (active == 4) {
      url = '/huahua/my_open';
      datas = {
        page: page
      }
    }
    app.util.request({
      url: url,
      data: datas
    }).then((res) => {
      if (res.data.list.length > 0) {
        this.setData({
          itemList: itemList.concat(res.data.list),
          noMore: 0,
          page: page + 1
        });
      } else {
        this.setData({
          noMore: 1
        });
      }
    });
  },
  refreshList: function () {
    this.setData({
      page: 1,
      itemList: []
    });
    this.getList();
  },
  onPullDownRefresh() {
    this.refreshList();
    setTimeout(() => {
      wx.hideNavigationBarLoading(); //完成停止加载
      wx.stopPullDownRefresh(); //停止下拉刷新
    }, 500);
  },
  onReachBottom() {
    this.getList();
  },
  tabs(e) {
    let num = e.currentTarget.dataset.num;
    if (num == 1) {
      wx.reLaunch({
        url: '/pages/index/index',
      })
    } else {
      wx.navigateTo({
        url: '/pages/draw/index',
      })
    }
  },
  cut(e) {
    let num = e.currentTarget.dataset.num;
    this.setData({
      active: num
    })
    this.refreshList();
  },
  message(){//消息
    wx.navigateTo({
      url: '/pages/user/message/message',
    })
  },
  sets() {//管理中心
    wx.navigateTo({
      url: '/pages/user/set/set',
    })
  },
  balance(){//账号余额
    wx.navigateTo({
      url: '/pages/user/balance/balance',
    })
  },
  glod() {//充值中心
    wx.navigateTo({
      url: '/pages/task/invest?power='+this.data.userinfo.power,
    })
  },

  getUserInfo() {
    app.util.request({
      url: '/user/getInfo'
    }).then(res => {
      wx.setStorageSync('userid',res.data.id);
      wx.setStorageSync('server',res.data.server);
      this.setData({
        userinfo: res.data
      })
    })
  },
  getUserProfile() {
    wx.getUserProfile({
      desc: '使用微信登录'
    }).then(e => {
      if (!(e.errMsg.indexOf('fail') > 0)) {
        app.util.request({
          url: '/user/setUserAvatar',
          data: {
            encryptedData: e.encryptedData,
            iv: e.iv
          }
        }).then(res => {
          this.getUserInfo()
        })
      } else {
        app.util.message('授权失败', 'error')
      }
    }).catch(() => {

    })
  },
  getUserPhone(e) {
    if (e.detail.errMsg != 'getPhoneNumber:ok') {
      var message = e.detail.errMsg
      if (message.indexOf('user deny') !== -1) {
        message = '已取消授权'
      }
      app.util.message(message, 'error')
      return
    }
    app.util.request({
      url: '/user/setUserPhone2',
      data: {
        code:e.detail.code
      }
    }).then(res => {
      this.getUserInfo()
    })
  },

  linkto(e) {
    const url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url
    })
  },
  toPayVip() {
    wx.navigateTo({
      url: '/pages/pay/vip'
    })
  },
  toTask() {
    wx.switchTab({
      url: '/pages/task/index'
    })
  },
  toInputNickname() {
    wx.navigateTo({
      url: '/pages/user/nickname'
    })
  },
  detail(e) {
    let index = e.currentTarget.dataset.index;
    if(this.data.active==1 || this.data.active==4){
      wx.navigateTo({
        url: '/pages/draw/detail?id=' + this.data.itemList[index].id,
      })
    }else{
      wx.navigateTo({
        url: '/pages/draw/detail?id=' + this.data.itemList[index].zuopin.id
      })
    }
  },
  delet(e) {
    let index = e.currentTarget.dataset.index;
    let id = '';
    if(this.data.active==1){//作品id
      id = this.data.itemList[index].id;
      app.util.request({
        url: '/huahua/delete_hh',
        data:{id:id}
      }).then(res => {
        app.util.message('删除成功');
        this.refreshList();
      })
    }else if(this.data.active==2 ||　this.data.active==3){//喜欢或收藏
      id = this.data.itemList[index].zuopin.id;
      let type = this.data.active - 1;
      // console.log(id,type)
      app.util.request({
        url: '/user/like',
        data:{zid:id,type:type}
      }).then(res => {
        app.util.message('删除成功');
        this.refreshList();
      })
    }else if(this.data.active==4){//公开
      id = this.data.itemList[index].id;
      app.util.request({
        url: '/user/open',
        data:{zid:id}
      }).then(res => {
        app.util.message('删除成功');
        this.refreshList();
      })
    }
  },
})